package objectOriented

object Constructor {
  def main(args: Array[String]): Unit = {
    /*val person = new Person("aqie", 18)
    println(person.name + ": " + person.age + ": " + person.school)

    val person2 = new Person("啊切", 25, "M")
    println(person2.name + ": " + person2.age + ": " + person2.school)*/

    var student = new Student("aqie123",10, "electronic")
    println(student.name + ": " + student.age + ": " + student.major)

    println(student)
  }

}

class Person222(val name:String, val age:Int){     // 主构造器
  println("class init")

  val school = "ustc"
  var gender:String = _
  // 附属构造器, 第一行必须调用主构造器或其他附属构造器
  def this(name:String, age:Int, gender:String){
    this(name, age)
    this.gender = gender
  }
  println("class destroy")
}

// 调用子类会先执行父类构造方法
class Student(name:String, age:Int, var major:String) extends Person222(name, age) {     // 添加var 才能被外部访问
  println("class student init")

  // 重写父类属性
  override val school: String = "hust"
  override def toString:String = "override toString " + school
  println("class student destroy")
}